package MainUI;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.File;

public class TitleBlock {
    private static BorderPane titleBlock = new BorderPane();
    //设置程序的名字和程序图片
    private File imagePath = new File("image");
    private Image stageImage = new Image("file:" + imagePath.getAbsolutePath() + "/stage.png");
    private Image stageName = new Image("file:" + imagePath.getAbsolutePath() + "/stageName.png");

    public TitleBlock(Stage primaryStage) {
        //设置标题栏
        HBox title = new HBox();
        title.getChildren().addAll(new ImageView(stageImage), new ImageView(stageName));
        //重新构建全屏缩小关闭
        Image image = new Image("file:" + imagePath.getAbsolutePath() + "/-.png");
        Button minButton = new Button();
        minButton.setGraphic(new ImageView(image));
        image = new Image("file:" + imagePath.getAbsolutePath() + "/O.png");
        Button maxButton = new Button("");
        maxButton.setGraphic(new ImageView(image));
        image = new Image("file:" + imagePath.getAbsolutePath() + "/X.png");
        Button closeButton = new Button("");
        closeButton.setGraphic(new ImageView(image));
        minButton.setStyle("-fx-base: #fec5c9; -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1;-fx-text-fill: white ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        maxButton.setStyle("-fx-base: #fec5c9; -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1;-fx-text-fill: white ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        closeButton.setStyle("-fx-base: #fec5c9; -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1;-fx-text-fill: red ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        minButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.setIconified(true);
            }
        });
        maxButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.setMaximized(!primaryStage.isMaximized());
            }
        });
        closeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.close();
            }
        });
        HBox hBox = new HBox();
        hBox.getChildren().addAll(minButton, maxButton, closeButton);
        titleBlock.setStyle("-fx-background-color:#fec5c9");
        titleBlock.setLeft(title);
        titleBlock.setRight(hBox);
        DragWindowHandler dragWindowHandler = new DragWindowHandler(primaryStage);
        titleBlock.setOnMousePressed(dragWindowHandler);/* 鼠标按下 */
        titleBlock.setOnMouseDragged(dragWindowHandler);/* 鼠标拖动 */
    }

    public static BorderPane getTitleBlock() {
        return titleBlock;
    }
}
